ARG HADOOP_VERSION
ARG HIVE_VERSION
ARG TEZ_VERSION
ARG BASE_OS

FROM registry.openanolis.cn/openanolis/anolisos:23 as downloader
ARG HADOOP_VERSION
ARG HIVE_VERSION
ARG TEZ_VERSION

RUN yum install -y tar gzip
ENV GOSU_VERSION 1.17
RUN set -x; \
    arch=$(uname -m); \
    case "${arch}" in \
    "x86_64" ) \
    NATIVE_BEELINE_ARCH="x86" \
    GOSU_ARCH="amd64" \
    ;; \
    "aarch64" ) \
    NATIVE_BEELINE_ARCH="arm" \
    GOSU_ARCH="arm64" \
    ;; \
    * ) \
    echo unsupported arch : ${arch}; exit 1 \
    ;; \
    esac; \
    curl -o nativebeeline https://cr-images-pub.oss-cn-hangzhou.aliyuncs.com/root/hive/nativeBeeline-${HIVE_VERSION}-${NATIVE_BEELINE_ARCH}; \
    curl -o gosu https://cr-images-pub.oss-cn-hangzhou.aliyuncs.com/root/hive/gosu-${GOSU_VERSION}-${GOSU_ARCH}

RUN curl -O -m 600 https://cr-images-pub.oss-cn-hangzhou.aliyuncs.com/root/hive/apache-hive-${HIVE_VERSION}-bin.tar.gz
RUN gzip -d apache-hive-${HIVE_VERSION}-bin.tar.gz
RUN tar xf apache-hive-${HIVE_VERSION}-bin.tar

RUN curl -O -m 600 https://cr-images-pub.oss-cn-hangzhou.aliyuncs.com/root/hive/apache-tez-${TEZ_VERSION}-bin.tar.gz
RUN gzip -d apache-tez-${TEZ_VERSION}-bin.tar.gz
RUN tar xf apache-tez-${TEZ_VERSION}-bin.tar

RUN curl -O -m 600 https://cr-images-pub.oss-cn-hangzhou.aliyuncs.com/root/hive/hadoop-${HADOOP_VERSION}.tar.gz
RUN gzip -d hadoop-${HADOOP_VERSION}.tar.gz
RUN tar xf hadoop-${HADOOP_VERSION}.tar

RUN mv apache-hive-${HIVE_VERSION}-bin hive
RUN mv apache-tez-${TEZ_VERSION}-bin tez
RUN mv hadoop-${HADOOP_VERSION} hadoop

RUN rm -rf hive/jdbc/*
RUN rm -rf tez/share/*
RUN rm -rf hadoop/share/doc/*
RUN mkdir -p hive/data/warehouse

COPY hive-site.xml hive/conf/

FROM ${BASE_OS}
ARG HADOOP_VERSION
ARG HIVE_VERSION
ARG TEZ_VERSION
ARG JAVA_VERSION
ARG OS_SUFFIX
ARG UID=1000

LABEL org.opencontainers.image.title="Hive" \
    org.opencontainers.image.version="${HIVE_VERSION}-${OS_SUFFIX}"

RUN set -eux; \
    case "${JAVA_VERSION}" in \
    "8" ) \
    yum install -y java-1.8.0-alibaba-dragonwell \
    ;; \
    * ) \
    echo unsupported java version: ${JAVA_VERSION}; exit 1 \
    ;; \
    esac; \
    yum clean all; \
    groupadd --system --gid=${UID} hive; \
    useradd --create-home --system --uid=${UID} --gid=hive hive; \
    chown hive:hive /home/hive

ENV HADOOP_HOME=/opt/hadoop \
    HADOOP_COMMON_HOME=/opt/hadoop \
    HADOOP_HDFS_HOME=/opt/hadoop \
    HADOOP_MAPRED_HOME=/opt/hadoop \
    HADOOP_YARN_HOME=/opt/hadoop \
    HIVE_HOME=/opt/hive \
    TEZ_HOME=/opt/tez \
    HIVE_VERSION=${HIVE_VERSION} \
    JAVA_HOME=/etc/alternatives/jre

ENV PATH=${HIVE_HOME}/bin:${HADOOP_HOME}/bin:$PATH

COPY --from=downloader --chown=hive:hive hadoop /opt/hadoop
COPY --from=downloader --chown=hive:hive hive /opt/hive
COPY --from=downloader --chown=hive:hive tez /opt/tez
COPY --from=downloader --chown=hive:hive nativebeeline /opt/hive/bin/nativebeeline
COPY --from=downloader --chown=hive:hive gosu /usr/local/bin/gosu
RUN chmod +x /opt/hive/bin/nativebeeline

RUN set -eux; \
    yum -y update; \
    yum -y install mysql mysql-server wget; \
    yum clean all


RUN set -eux; \
    export GNUPGHOME="$(mktemp -d)"; \
    chmod +x /usr/local/bin/gosu; \
    gosu --version; \
    gosu nobody true

RUN set -eux; \
    rm -rf /var/lib/mysql; \
    mkdir -p /var/lib/mysql /var/run/mysqld; \
    chown -R mysql:mysql /var/lib/mysql /var/run/mysqld; \
    chmod 766 -R /var/log/mysql; \
    chmod 1777 /var/run/mysqld /var/lib/mysql; \
    mkdir /docker-entrypoint-initdb.d; \
    # Avoid mysqld permission denied. Docker scenaio do not need this.
    setcap -r /usr/libexec/mysqld;\
    mysqld --version; \
    mysql --version

ENV MYSQL_MAJOR 8.0

ENV MYSQL_VERSION 8.0.30

VOLUME /var/lib/mysql

COPY mysql-entrypoint.sh /usr/local/bin/

COPY config/ /etc/mysql/

RUN set -eux; \
    ln -s usr/local/bin/mysql-entrypoint.sh /mysql-entrypoint.sh; \
    mv /etc/my.cnf.d/* /etc/mysql/conf.d/; \
    rm -rf /etc/my.cnf /etc/my.cnf.d/

RUN curl -o /opt/hive/lib/mysql-connector-java-5.1.47.jar https://cr-images-pub.oss-cn-hangzhou.aliyuncs.com/root/hive/mysql-connector-java-5.1.47.jar
COPY entrypoint.sh /
RUN chmod 755 entrypoint.sh
RUN chmod 755 /mysql-entrypoint.sh

WORKDIR /opt/hive
EXPOSE 10000 10002 9083 3306 33060

ENTRYPOINT ["/entrypoint.sh"]